package com.fr.base.core.image4j.codec.ico;

import com.fr.base.core.image4j.codec.bmp.BMPDecoder;
import com.fr.base.core.image4j.codec.bmp.ColorEntry;
import com.fr.base.core.image4j.codec.bmp.InfoHeader;
import com.fr.base.core.image4j.io.LittleEndianInputStream;
import java.awt.image.BufferedImage;
import java.awt.image.WritableRaster;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import javax.imageio.ImageIO;
import javax.imageio.ImageReader;

public class ICODecoder
{
  private static final int PNG_MAGIC = -1991225785;
  private static final int PNG_MAGIC_LE = 1196314761;
  private static final int PNG_MAGIC2 = 218765834;
  private static final int PNG_MAGIC2_LE = 169478669;

  public static List read(File paramFile)
    throws IOException
  {
    return read(new FileInputStream(paramFile));
  }

  public static List readExt(File paramFile)
    throws IOException
  {
    return readExt(new FileInputStream(paramFile));
  }

  public static List read(InputStream paramInputStream)
    throws IOException
  {
    List localList = readExt(paramInputStream);
    ArrayList localArrayList = new ArrayList(localList.size());
    for (int i = 0; i < localList.size(); ++i)
    {
      ICOImage localICOImage = (ICOImage)localList.get(i);
      BufferedImage localBufferedImage = localICOImage.getImage();
      localArrayList.add(localBufferedImage);
    }
    return localArrayList;
  }

  public static List readExt(InputStream paramInputStream)
    throws IOException
  {
    LittleEndianInputStream localLittleEndianInputStream = new LittleEndianInputStream(paramInputStream);
    int i = localLittleEndianInputStream.readShortLE();
    int j = localLittleEndianInputStream.readShortLE();
    int k = localLittleEndianInputStream.readShortLE();
    IconEntry[] arrayOfIconEntry = new IconEntry[k];
    for (int l = 0; l < k; l = (short)(l + 1))
      arrayOfIconEntry[l] = new IconEntry(localLittleEndianInputStream);
    l = 0;
    ArrayList localArrayList = new ArrayList(k);
    try
    {
      for (l = 0; l < k; ++l)
      {
        Object localObject1;
        Object localObject2;
        Object localObject3;
        Object localObject4;
        Object localObject5;
        Object localObject6;
        Object localObject7;
        Object localObject8;
        Object localObject9;
        int i1 = localLittleEndianInputStream.readIntLE();
        if (i1 == 40)
        {
          int i14;
          int i15;
          InfoHeader localInfoHeader = BMPDecoder.readInfoHeader(localLittleEndianInputStream, i1);
          localObject1 = new InfoHeader(localInfoHeader);
          ((InfoHeader)localObject1).iHeight = (localInfoHeader.iHeight / 2);
          localObject2 = new InfoHeader(localInfoHeader);
          ((InfoHeader)localObject2).iHeight = ((InfoHeader)localObject1).iHeight;
          ((InfoHeader)localObject1).sBitCount = 1;
          ((InfoHeader)localObject1).iNumColors = 2;
          BufferedImage localBufferedImage = BMPDecoder.read((InfoHeader)localObject2, localLittleEndianInputStream);
          localObject3 = new BufferedImage(((InfoHeader)localObject2).iWidth, ((InfoHeader)localObject2).iHeight, 2);
          localObject4 = new ColorEntry[] { new ColorEntry(255, 255, 255, 255), new ColorEntry(0, 0, 0, 0) };
          if (localInfoHeader.sBitCount == 32)
          {
            int i4 = arrayOfIconEntry[l].iSizeInBytes;
            int i5 = localInfoHeader.iSize;
            int i6 = ((InfoHeader)localObject2).iWidth * ((InfoHeader)localObject2).iHeight * 4;
            int i7 = i4 - i5 - i6;
            int i8 = localLittleEndianInputStream.skipBytes(i7);
            i9 = i7;
            while (i8 < i9)
            {
              if (i8 < 0)
                throw new IOException("Failed to read [skip]");
              i9 = i7 - i8;
              i8 = localLittleEndianInputStream.skipBytes(i9);
            }
            WritableRaster localWritableRaster1 = localBufferedImage.getRaster();
            WritableRaster localWritableRaster2 = localBufferedImage.getAlphaRaster();
            WritableRaster localWritableRaster3 = ((BufferedImage)localObject3).getRaster();
            WritableRaster localWritableRaster4 = ((BufferedImage)localObject3).getAlphaRaster();
            i14 = ((InfoHeader)localObject2).iHeight - 1;
            while (true)
            {
              if (i14 < 0)
                break label679;
              for (i15 = 0; i15 < ((InfoHeader)localObject2).iWidth; ++i15)
              {
                int i16 = localWritableRaster1.getSample(i15, i14, 0);
                int i17 = localWritableRaster1.getSample(i15, i14, 1);
                int i18 = localWritableRaster1.getSample(i15, i14, 2);
                int i19 = localWritableRaster2.getSample(i15, i14, 0);
                localWritableRaster3.setSample(i15, i14, 0, i16);
                localWritableRaster3.setSample(i15, i14, 1, i17);
                localWritableRaster3.setSample(i15, i14, 2, i18);
                localWritableRaster4.setSample(i15, i14, 0, i19);
              }
              --i14;
            }
          }
          localObject5 = BMPDecoder.read((InfoHeader)localObject1, localLittleEndianInputStream, localObject4);
          localObject6 = localBufferedImage.getRaster();
          localObject7 = ((BufferedImage)localObject3).getRaster();
          localObject8 = ((BufferedImage)localObject3).getAlphaRaster();
          localObject9 = ((BufferedImage)localObject5).getRaster();
          for (int i9 = 0; i9 < ((InfoHeader)localObject2).iHeight; ++i9)
            for (int i10 = 0; i10 < ((InfoHeader)localObject2).iWidth; ++i10)
            {
              i14 = localBufferedImage.getRGB(i10, i9);
              int i11 = i14 >> 16 & 0xFF;
              int i12 = i14 >> 8 & 0xFF;
              int i13 = i14 & 0xFF;
              ((WritableRaster)localObject7).setSample(i10, i9, 0, i11);
              ((WritableRaster)localObject7).setSample(i10, i9, 1, i12);
              ((WritableRaster)localObject7).setSample(i10, i9, 2, i13);
              i15 = ((BufferedImage)localObject5).getRGB(i10, i9);
              ((WritableRaster)localObject8).setSample(i10, i9, 0, i15);
            }
          localObject5 = arrayOfIconEntry[l];
          localObject6 = new ICOImage((BufferedImage)localObject3, localInfoHeader, (IconEntry)localObject5);
          ((ICOImage)localObject6).setPngCompressed(false);
          ((ICOImage)localObject6).setIconIndex(l);
          localArrayList.add(localObject6);
        }
        else if (i1 == 1196314761)
        {
          int i2 = localLittleEndianInputStream.readIntLE();
          if (i2 != 169478669)
            throw new IOException("Unrecognized icon format for image #" + l);
          localObject1 = arrayOfIconEntry[l];
          localObject2 = new byte[((IconEntry)localObject1).iSizeInBytes - 8];
          int i3 = localLittleEndianInputStream.read(localObject2);
          if (i3 != localObject2.length)
            throw new IOException("Unable to read image #" + l + " - incomplete PNG compressed data");
          localObject3 = new ByteArrayOutputStream();
          localObject4 = new DataOutputStream((OutputStream)localObject3);
          ((DataOutputStream)localObject4).writeInt(-1991225785);
          ((DataOutputStream)localObject4).writeInt(218765834);
          ((DataOutputStream)localObject4).write(localObject2);
          localObject5 = ((ByteArrayOutputStream)localObject3).toByteArray();
          localObject6 = new ByteArrayInputStream(localObject5);
          localObject7 = ImageIO.createImageInputStream(localObject6);
          localObject8 = getPNGImageReader();
          ((ImageReader)localObject8).setInput(localObject7);
          localObject9 = ((ImageReader)localObject8).read(0);
          IconEntry localIconEntry = arrayOfIconEntry[l];
          ICOImage localICOImage = new ICOImage((BufferedImage)localObject9, null, localIconEntry);
          localICOImage.setPngCompressed(true);
          localICOImage.setIconIndex(l);
          localArrayList.add(localICOImage);
        }
        else
        {
          throw new IOException("Unrecognized icon format for image #" + l);
        }
      }
    }
    catch (IOException localIOException)
    {
      label679: throw new IOException("Failed to read image # " + l);
    }
    return ((List)(List)(List)(List)(List)(List)(List)(List)(List)localArrayList);
  }

  private static ImageReader getPNGImageReader()
  {
    ImageReader localImageReader = null;
    Iterator localIterator = ImageIO.getImageReadersByFormatName("png");
    if (localIterator.hasNext())
      localImageReader = (ImageReader)localIterator.next();
    return localImageReader;
  }
}